package com.itgaoshu.hospitalproject.mapper;

import com.itgaoshu.hospitalproject.bean.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
@Mapper
public interface ReportMapper {
    //查询所有挂号病人
    @Select("select r.reportId,r.reportName,DATE_FORMAT(r.time,'%Y-%m-%d') datime,substr(r.carid,1,6) carido, substr(r.carid,16,18) caridt,r.phone,r.sex,r.age,p.department,d.doctorName,r.price,e.type,r.time,r.users,r.state,e.registeredid dregisteredid,d.doctorid ddoctorid,p.departmentId ddepartmentid from report r,registeredtype e,doctor d,departments p where state=1 and r.reportType=e.registeredId and r.doctor=d.doctorId and r.department=p.departmentId")
    List<ReportVo> selectAllReportVo();

    //查询所有科室
    @Select("select * from departments")
    List<CDepartments> selectAllCDe();

    //查询所有的挂号类型
    @Select("select * from registeredtype")
    List<CRegisteredtype> selectAllReg();

    //查询医生
    @Select("select * from doctor")
    List<Doctor> selectAllDoc();

    //检查身份证是否唯一
    @Select("select count(*) from report where carid=#{carid} and state=1")
    int selectCcarid(String carid);

    //检查手机号唯一
    @Select("select count(*) from report where phone=#{phone} and state=1")
    int selectPhone(String phone);

    //根据医生查询挂号费
    @Select("select price from registeredtype where registeredId=#{registeredId}")
    int seltymo(int registeredId);
    //挂号添加病人
    @Update("insert into report values(#{reportId},#{reportName},#{sex},#{age},#{department},#{doctor},#{reporttype},#{price},sysdate(),#{users},1,#{phone},#{carid},#{zhuan})")
    int addReport(CReport reportVo);

    //删除挂号病人
    @Update("update report set state=0 where reportid=#{id}")
    int deleteReport(int id);


    //转入住院部
    @Update("update report set zhuan=#{zhuan},state=4 where reportid=#{id} ")
    int updateReport(String zhuan,Integer id);

    //查询该用户下是否还有未付费药品
    @Select("SELECT SUM(repicetotal) FROM cashier WHERE reportId=#{reportId} and state=1")
    Double selectsumrep(int reportId);

    //查询该用户下是否还有未交钱的项目
    @Select("SELECT count(mstate) FROM cashier WHERE reportId=#{reid} AND mstate=0 AND state=1")
    int selectxiangmu(int reid);


    @Select("SELECT count(jie) FROM cashier WHERE reportId=#{reid} AND jie!='' AND mstate=1 AND ostate=1")
    int selectyi(int reid);

    @Select("SELECT COUNT(cashier) FROM cashier WHERE reportId=#{reid} AND mstate=1 AND ostate=1 AND state=1")
    int selectgong(int reid);
}
